package com.andy.module.network

import com.andy.network.interceptor.ResponseBodyInterceptor
import okhttp3.Response


/**
 * 作者　: luofan
 * 时间　: 2021/6/9
 * 描述　: 添加解析是没有content字段的容错
 */
class BodyInterceptor : ResponseBodyInterceptor() {

    override fun intercept(response: Response, url: String, body: String): Response {
        if (!body.startsWith("{")) {
            return response
        }
//        val obj = JSONObject(body)
//        if (body.contains("errorCode")) {
//            val errorCode = obj.getString("errorCode")
//            if (errorCode == tokenErrorCode || errorCode == tokenOutCode) {
//                //处理错误
//                return response
//            }
//        }
        //添加容错,content本地处理的默认不能为空,但是服务器某些可能不会返回(接口出错等)
//        if (!body.contains("content")) {
//            obj.put("content", JSONObject())
//            val contentType: MediaType? = response.body()!!.contentType()
//            val responseBody: ResponseBody = ResponseBody.create(contentType, obj.toString())
//            return response.newBuilder().body(responseBody).build()
//        }
        return response
    }


}